﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using GeneralDFS.DataEntity.Config;
using System.Threading;

namespace GeneralDFS.BusinessLogical
{
    public class FSVolume
    {
        private static FSVolume instance;

        public static FSVolume GetInstance()
        {
            if(instance!=null)
            {
                return instance;
            }
            throw new Exception("uninit instance");
        }
        public FsDir Root;

        public static void Init(string path)
        {
            instance=new FSVolume(path);
        }

        private FSVolume(string path)
        {
            Root = new FsDir(path);
        }

        /// <summary>
        /// 获得有空闲空间的文件目录
        /// </summary>
        /// <param name="guid">文件块名</param>
        /// <returns></returns>
        public FsDir GetFreeDir(Guid guid)
        {
            FsDir current = Root;
            if (current.IsFree())
                return Root;
            char[] c = guid.ToString().ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if(current.Children==null)
                    current.AddSurDir();
                else if(current.Children.Count!=16)
                {
                    while (current.Children.Count!=16)
                    {
                        Thread.Sleep(1);
                    }
                }
                int index = FsDir.GetChildrenIndex(c[i]);
                if (index >= 0)
                {
                    current = current.Children[index];
                    if (current.IsFree())
                    {
                        return current;
                    }
                }
            }
            return null;
        }
        /// <summary>
        /// 将完成的临时文件存放到数据区
        /// </summary>
        /// <param name="sourceFilePath">原文件的地址</param>
        /// <param name="guid">文件的Guid</param>
        /// <returns>返回文件块的地址</returns>
        public string MoveTempFileToFreeDirectory(string sourceFilePath,Guid guid)
        {
            FsDir fsDir = GetFreeDir(guid);
            return fsDir.MoveBlockToCurrentDirectory(sourceFilePath,guid);
        }

        public string GetFilePath(Guid guid)
        {
            FsDir current = Root;
            if (current.Exist(guid))
            {
                return Root.BlockName(guid);
            }
            char[] c = guid.ToString().ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (current.Children == null)
                {
                    return null;
                }
                else
                {
                    int index = FsDir.GetChildrenIndex(c[i]);
                    if (index >= 0)
                    {
                        current = current.Children[index];
                        if (current.Exist(guid))
                            return current.BlockName(guid);
                    }
                }
            }
            return null;
        }
        public int GetTotalBlockNum()
        {
            int num = 0;
            FileInfo[] file = Root.CurrentDirectory.GetFiles("blk_*", SearchOption.AllDirectories);
            return file.Length/2;
        }
    }
}
